package com.ck.roomdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;

import android.os.Bundle;
import android.view.View;

import com.ck.roomdemo.databinding.ActivityMainBinding;
import com.ck.roomdemo.room.MyDatabase;
import com.ck.roomdemo.room.Student;
import com.ck.roomdemo.room.StudentDao;

import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private MyViewModel viewModel;
    private MyDatabase myDatabase;
    private StudentDao studentDao;
    private RecyclerView recyclerView;
    private MyAdapter myAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        viewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(this.getApplication())).get(MyViewModel.class);

        myAdapter = new MyAdapter();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(myAdapter);

        viewModel.getLiveData().observe(this, new Observer<List<Student>>() {
            @Override
            public void onChanged(List<Student> students) {
                myAdapter.setStudents(students);
                myAdapter.notifyDataSetChanged();
//                String text = "";
//                Student student;
//                for (int i = 0; i < students.size(); i++) {
//                    student = students.get(i);
//                    text += student.getId() + ": " + student.getName() + ": " + student.getAge() + ": " + student.getSex() + "\n";
//                }
            }
        });

        binding.clearBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.clear();
            }
        });
        binding.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student student = new Student("Qinly", 20, 1);
                student.setId(15);
                viewModel.delete(student);
            }
        });
        binding.updateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student student = new Student("Qinly", 20, 1);
                student.setId(15);
                viewModel.update(student);
            }
        });
        binding.insertBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Student student1 = new Student("ck", 32, 0);
                Student student2 = new Student("ck2", 30, 1);
                viewModel.insert(student1, student2);
            }
        });
    }
}